import { commonApi, getDictionaries } from '@/api/fileCommonApi.js'
import commonDialog from '../components/commonDialog.vue'
export default {
  data() {
    return {
      pageNo: 0,
      limit: 5,
      pageTotal: 0,
      list: [],
      showModel: false,
      moduleType: '',
      formSearch: {
        searchTxt: ''
      },
      rules: {
        searchTxt: [
          { required: true, message: '请输入关键词' }
        ]
      }
    }
  },
  components: {
    commonDialog

  },
  watch: {

  },
  beforeMount() {
  },
  beforeDestroy() {
  },
  mounted() {
    this.getList()
  },
  methods: {
    search() {
      this.$refs['searchForm'].validate((valid) => {
        if (valid) {
          this.pageNo = 0
          this.getList()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handler(type, id) {
      var energy = ''
      this.moduleType = type
      this.showModel = false
      if (type === 'see' || type === 'edit') {
        var data = { id }
        energy = 'detail'
        commonApi({ type: this.type, energy, data }).then((res) => {
          this.showModel = true
          // this.$refs['commonDialog'].clearValidate()
          this.$refs['commonDialog'].renderpage(res.data)
        })
      } else {
        this.showModel = true
        // this.$refs['commonDialog'].clearValidate()
        this.$refs['commonDialog'].renderpage({
          name: '',
          status: ''
        })
      }
    },

    // 获取素材
    async getMaterial() {
      return getDictionaries(12)
    },
    editTable(obj) {
      // var typeStr = this.type === 'powerType' ? 'name' : 'cate_name'
      // var data = {
      //   id: obj.id,
      //   status: obj.status
      // }
      // data[typeStr] = obj.name
      var data = obj
      commonApi({ type: this.type, energy: 'update', data }).then((res) => {
        // console.log(res)
        this.$refs['commonDialog'].reSet()
        this.showModel = false
        this.moduleType = ''
        this.getList()
      })
    },
    addTable(obj) {
      var data = obj
      commonApi({ type: this.type, energy: 'create', data }).then((res) => {
        console.log(res)
        this.$refs['commonDialog'].reSet()
        this.showModel = false
        this.moduleType = ''
        this.getList()
      })
    },
    closeDialog() {
      this.showModel = false
      this.moduleType = ''
    },
    deleteItem(v, n) {
      console.log(v)
      console.log(n)
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var data = { id: v.id }
        commonApi({ type: this.type, energy: 'delete', data }).then((res) => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          // this.list.splice(n,1)
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    getList(obj) {
      console.log(this.type)
      var typeStr = this.type === 'groupManage' ? 'keyword' : 'name'
      var data = {
        offset: this.pageNo,
        limit: this.limit,
        sort: '',
        order: ''
      }
      data[typeStr] = this.formSearch.searchTxt
      if (obj) {
        data.sort = obj.sort || ''
        data.order = obj.order || ''
        data[typeStr] = obj[typeStr] || ''
      }
      console.log(this.type)
      commonApi({ type: this.type, energy: 'list', data }).then((res) => {
        console.log(res)
        this.pageTotal = res.data.total
        console.log(this.pageTotal)
        for (var i = 0; i < res.data.list.length; i++) {
          res.data.list[i].status_txt = res.data.list[i].status === 1 ? '正常' : '禁用'
        }
        this.list = res.data.list
      })
    },
    changePage(e) {
      console.log(e)
      this.pageNo = e - 1
      this.getList()
    }
  }
}
